<?php
/**
 * Created by PhpStorm.
 * User: sunshine
 * Date: 17-5-9
 * Time: 上午1:56
 */

namespace Admin\Model;


use Think\Model;

class StudentCollegeModel extends Model{
    protected $tableName = 'student_college';
    public function getCollege(){
        $result = $this->getField('cid,collegeName');
        if ($result){
            return array(
              'status'=>true,
                'info'=>$result
            );
        }else{
            return array(
                'status'=>false,
                'errcode'=>107,
                'errmsg'=>'read error'
            );
        }
    }
}

class StudentSpecialtyModel extends Model
{
    protected $tableName = 'student_specialty';

    public function getSpecialty($cid)
    {
        $result = $this->where('cid="' . $cid . '"')->select();
        if ($result) {
            return array(
                'status' => true,
                'info' => $result
            );
        } else {
            return array(
                'status' => false,
                'errcode' => 107,
                'errmsg' => 'read error'
            );
        }
    }
}
class StudentModel extends Model
{
    function addStudent($student)
    {
        if ($this->where('sid="' . $student['sid'] . '"')->find()) {
            return array(
                'status' => false,
                'errcode' => 110,
                'errmsg' => 'had studentname'
            );
        } else {
            $result = $this->add($student);
            if ($result) {
                return array(
                    'status' => true
                );
            } else {
                return array(
                    'status' => false
                );
            }
        }
    }

    function findStudent($sid, $index)
    {
        $where['sid'] = array('LIKE', '%' . (string)$sid . '%');
        $result = $this->order('id desc')->field('sid,stuName')->where($where)->limit($index * 10, 10)->select();
        if ($result) {
            return array(
                'status' => true,
                'info' => $result
            );
        } else {
            return array(
                'status' => false,
                'errcode' => 101,
                'errmsg' => 'no user'
            );
        }
    }

    function deleteStudent($sid)
    {
        $result = $this->where('sid ="' . (string)$sid . '"')->delete();
        if ($result) {
            return array(
                'status' => true,
                'info' => $result
            );
        } else {
            return array(
                'status' => false,
                'errcode' => 101,
                'errmsg' => 'no user'
            );
        }
    }

    function getStudent($sid)
    {
        $result = $this->where('sid ="' . (string)$sid . '"')->find();
        if ($result) {
            return array(
                'status' => true,
                'info' => $result
            );
        } else {
            return array(
                'status' => true,
                'errcode' => 101,
                'errmsg' => 'no user'
            );
        }
    }

    function updateStudent($student)
    {
        $result = $this->where('sid ="' . (string)$student['sid'] . '"')->save($student);
        if ($result) {
            return array(
                'status' => true,
                'info' => $result
            );
        } else {
            return array(
                'status' => false,
                'errcode' => 101,
                'errmsg' => 'no user'
            );
        }
    }

    function getCollege(){
        $studentcollegemodel = new StudentCollegeModel();
        return $studentcollegemodel->getCollege();
    }

    function getSpecialty($cid){
        $studentspecialty = new StudentSpecialtyModel();
        return $studentspecialty->getSpecialty($cid);
    }
}